﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

public class ObjectHelper {

    /// <summary>
    /// 获取枚举值的description
    /// </summary>
    /// <param name="value">枚举值</param>
    /// <param name="nameInstead">没有description时是否以name代替</param>
    /// <returns></returns>
    public static string GetDescription(Enum value, bool nameInstead = true) {
        Type type = value.GetType();
        string name = Enum.GetName(type, value);
        if (name == null) {
            return null;
        }
        FieldInfo field = type.GetField(name);
        DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
        if (attribute == null && nameInstead == true) {
            return name;
        }
        return attribute == null ? null : attribute.Description;
    }
}
